package com.liu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liu.Excel.ServeTrainItemEcharts;
import com.liu.Excel.ServeTrainItemExcel;
import com.liu.dao.ServeTrainDao;
import com.liu.dto.ServeTrainItemDto;
import com.liu.service.intf.ServeTrainService;
import com.liu.vo.PageBeanJson;
import com.liu.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ServeTrainServiceImpl implements ServeTrainService {
    @Autowired
    private ServeTrainDao serveTrainDao;

    @Override
    public PageBeanJson<ServeTrainItemDto> list(int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        List<ServeTrainItemDto> serveTrainItemDtos = serveTrainDao.select();
        PageInfo<ServeTrainItemDto> pageInfo = new PageInfo<>(serveTrainItemDtos);

        return new PageBeanJson<>(0, "ok", pageInfo.getTotal(), pageInfo.getList());
    }
    @Override
    public List<ServeTrainItemDto> selectAll() {
        return serveTrainDao.select();
    }
    @Override
    public PageBeanJson<ServeTrainItemDto> search(int page, int pageSize, ServeTrainItemDto serveTrainItemDto) {
        PageHelper.startPage(page, pageSize);
        List<ServeTrainItemDto> search = serveTrainDao.search(serveTrainItemDto);
        PageInfo<ServeTrainItemDto> pageInfo = new PageInfo<>(search);
        return new PageBeanJson<>(0, "ok", pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R insert(ServeTrainItemDto serveTrainItemDto) {
        int i = serveTrainDao.insertItem(serveTrainItemDto);
//        System.out.println("获取自增主键"+serveTrainItemDto.getServeTrainItem().getId());
//        System.out.println("打印一下现在的"+serveTrainItemDto);
        int insert = serveTrainDao.insert(serveTrainItemDto);
        int update = serveTrainDao.updateTrainItemId(serveTrainItemDto.getServeTrainItem().getId(),serveTrainItemDto.getId());
        return new R(insert, insert == 1 ? "ok" : "false", null);
    }
//   批量Excel批量添加
    @Override
    public R savaBatch(MultipartFile file) throws IOException {
        int inserts = -1;
        List<ServeTrainItemExcel> serveTrainItemExcels = new ArrayList<>();
        if (!file.isEmpty()){
            serveTrainItemExcels = EasyExcel.read(file.getInputStream(),ServeTrainItemExcel.class ,
                    new ReadListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {

                }
            }).sheet().doReadSync();
            System.out.println("ser:______"+serveTrainItemExcels);
            inserts = serveTrainDao.insertBatch(serveTrainItemExcels);
            serveTrainDao.insertBatch1(serveTrainItemExcels);
        }
        return new R(inserts,inserts==serveTrainItemExcels.size()?"ok":"false",null);
    }

    @Override
    public R trainSelect() {
        List<ServeTrainItemEcharts> list = serveTrainDao.selectTrainEcharts();
        System.out.println(list);
        //2.根据前端 组装数据
        Map<String,List<Object>> map=new HashMap<>();
        map.put("item",new ArrayList<>());//pid
        map.put("count0",new ArrayList<>());//总数0
        map.put("count1",new ArrayList<>());//总数1
        //3.组装数据  函数式编程
//        list.forEach(f->{
//
//        });
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getItem()==1){
                map.get("count0").add(list.get(i).getCount());
            }else {
                map.get("count1").add(list.get(i).getCount());
            }
            if (list.get(i).getItem()==0){
                map.get("item").add("不需要");
            }else {
                map.get("item").add("需要");
            }
        }
        return new R(0,"ok",map);
    }
    @Override
    public R update(ServeTrainItemDto serveTrainItemDto) {
        int update = serveTrainDao.update(serveTrainItemDto);
        return new R(update, update == 1 ? "ok" : "false", null);
    }

    @Override
    public R delete(int id, int t_id) {
        int delete = serveTrainDao.delete(id, t_id);
        return new R(delete, delete == 1 ? "ok" : "false", null);
    }

    @Override
    public R deleteBatch(int[] ids) {
        int i = serveTrainDao.delBatch(ids);
        return new R(i, i == 1 ? "ok" : "false", null);
    }


}
